﻿'Copyright © Etheral Studios 2012
'File created by Nick nick@etheralstudios.com
'File has been edited by:
'Nick nick@etheralstudios.com

Imports Ionic.Zip

Public Class MainForm

    Friend Shared Address As String
    Friend Shared MyIP As String

    Public Sub IPText_MouseClick(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles IPText.MouseClick
        IPText.Clear()
    End Sub

    Public Sub ContactSelect_Click(sender As System.Object, e As System.EventArgs) Handles ContactSelect.Click
        Dim Open As New OpenFileDialog()
        Open.Filter = "Comma Seperated Values File|*.csv"
        Open.InitialDirectory = "C:\ProgramData\WiN\Contacts"
        Open.CheckFileExists = True
        Open.ShowDialog(Me)
        Try
            Open.OpenFile()
            Dim src_read() As String = IO.File.ReadAllLines(Open.FileName)
            Dim firstLine As String = src_read(0)
            Dim data() As String = firstLine.Split(",")

            Address = data(2)
            IP.Text = Address

            Open.Dispose()

        Catch ex As Exception
        End Try
    End Sub

    Private Sub WiNToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles WiNToolStripMenuItem.Click
        If My.Computer.FileSystem.DirectoryExists("C:\ProgramData\WiN") Then
            Shell("C:\ProgramData\WiN\Server\apache_stop.bat")
            Shell("C:\ProgramData\WiN\Server\apache_start.bat")
        Else
            Dim reply As DialogResult = MessageBox.Show("A file (144MB) is needed to connect to WiN. Do you want to download that now?", "Download File?", _
              MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
            If reply = DialogResult.Yes Then
                My.Computer.Network.DownloadFile("http://content.WiN.net.nz/WiN_NT.zip", "C:\ProgramData")
                Dim ZipToUnpack As String = "C:\ProgramData\WiN_NT.zip"
                Dim UnpackDirectory As String = "C:\ProgramData"
                Using zip1 As ZipFile = ZipFile.Read(ZipToUnpack)
                    Dim ent As ZipEntry
                    For Each ent In zip1
                        ent.Extract(UnpackDirectory, ExtractExistingFileAction.OverwriteSilently)
                    Next
                End Using
                MessageBox.Show("The download is complete. Make sure to click Network > Connect to WiN to use WiN.", "Download Complete", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)
            End If
        End If
    End Sub

    Private Sub AddToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles AddToolStripMenuItem.Click
        NewContact.Show()
    End Sub

    Private Sub SendViaWiNToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles SendViaWiNToolStripMenuItem.Click
        Me.Hide()
        SMS.Show()
    End Sub

    Private Sub CallViaWiNToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles CallViaWiNToolStripMenuItem.Click
        Me.Hide()
        Voice.Show()
    End Sub

    Private Sub MainForm_Shown(sender As System.Object, e As System.EventArgs) Handles MyBase.Shown
        Me.Hide()
        enterip.Show()
    End Sub

    Private Sub EditToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles EditToolStripMenuItem.Click
        EditContact.Show()
    End Sub

    Private Sub AboutToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles AboutToolStripMenuItem.Click
        About.Show()
    End Sub

    Private Sub SelectButton_Click(sender As System.Object, e As System.EventArgs) Handles SelectButton.Click
        Address = IPText.Text
        IP.Text = Address
    End Sub

    Private Sub DocumentationToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles DocumentationToolStripMenuItem.Click
        Process.Start("http://WiN.net.nz/documentation")
    End Sub

    Private Sub MainForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        If My.Computer.FileSystem.DirectoryExists("C:\ProgramData\WiN") Then
        Else
            Dim reply As DialogResult = MessageBox.Show("A file (144MB) is needed to connect to WiN. Do you want to download that now?", "Download File?", _
              MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
            If reply = DialogResult.Yes Then
                My.Computer.Network.DownloadFile("http://content.WiN.net.nz/WiN_NT.zip", "C:\ProgramData\WiN_NT.zip")
                Dim ZipToUnpack As String = "C:\ProgramData\WiN_NT.zip"
                Dim UnpackDirectory As String = "C:\ProgramData"
                Using zip1 As ZipFile = ZipFile.Read(ZipToUnpack)
                    Dim ent As ZipEntry
                    For Each ent In zip1
                        ent.Extract(UnpackDirectory, ExtractExistingFileAction.OverwriteSilently)
                    Next
                End Using
            Else
                Me.Close()
            End If
        End If
    End Sub
End Class